package com.misyi.list;

import com.sun.tools.javac.util.Assert;

/**
 * 测试list
 *
 * @author licong
 * @date 2021-10-13 下午6:08
 */
public class TestList {

    public static void main(String[] args) {
//        testList(new ArrayList<>());
//        testList(new SingleLinkedList<>());
//        testList2(new LinkedList<>());
        testStack();
    }

    public static void testStack() {
        Stack<Integer> stack = new Stack<>();
        System.out.println(stack.size());

        stack.push(1);
        stack.push(2);
        stack.push(3);

        System.out.println(stack.top());
        System.out.println(stack.pop());
        System.out.println(stack.pop());
        System.out.println(stack.size());
        stack.clear();
        System.out.println(stack.size());
        System.out.println(stack.top());

    }

    public static void testList(List<Integer> list) {
        for (int i = 0; i < 11; i++) {
            list.add(i);
        }
        // 数组扩容: oldCapacity=10, newCapacity=15
        // 列表长度=11, 元素=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
        System.out.println(list);
        Assert.check("列表长度=11, 元素=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]".equals(list.toString()));

        // 列表长度=14, 元素=[11, 0, 1, 2, 13, 3, 4, 5, 6, 7, 8, 9, 10, 12]
        list.add(0, 11);
        list.add(list.size(), 12);
        list.add(4, 13);
        System.out.println(list);
        Assert.check("列表长度=14, 元素=[11, 0, 1, 2, 13, 3, 4, 5, 6, 7, 8, 9, 10, 12]".equals(list.toString()));

        // 列表长度=11, 元素=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
        list.remove(4);
        list.remove(list.size() -  1);
        list.remove(0);
        System.out.println(list);
        Assert.check("列表长度=11, 元素=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]".equals(list.toString()));

        // 列表长度=10, 元素=[0, 1, 2, 3, 4, 6, 7, 8, 9, 10]
        list.remove(5);
        System.out.println(list);
        Assert.check("列表长度=10, 元素=[0, 1, 2, 3, 4, 6, 7, 8, 9, 10]".equals(list.toString()));

        Assert.check(list.size() == 10);
        Assert.check(list.get(5) == 6);
        Assert.check(list.get(list.size() - 1) == 10);

        Assert.check(list.contains(6));
        Assert.check(!list.contains(5));

        list.set(3, 33);
        System.out.println(list);
        Assert.check(list.get(3) == 33);

        Assert.check(!list.isEmpty());

        list.clear();
        Assert.check(list.isEmpty());
    }


    public static void testList2(List<Integer> list) {
        for (int i = 0; i < 11; i++) {
            list.add(i);
        }
        // 数组扩容: oldCapacity=10, newCapacity=15
        // 列表长度=11, 元素=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
        System.out.println(list);

        // 列表长度=14, 元素=[11, 0, 1, 2, 13, 3, 4, 5, 6, 7, 8, 9, 10, 12]
        list.add(0, 11);
        list.add(list.size(), 12);
        list.add(4, 13);
        System.out.println(list);

        // 列表长度=11, 元素=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
        list.remove(4);
        System.gc();
        list.remove(list.size() -  1);
        System.gc();
        list.remove(0);
        System.gc();
        System.out.println(list);

        // 列表长度=10, 元素=[0, 1, 2, 3, 4, 6, 7, 8, 9, 10]
        list.remove(5);
        System.gc();
        System.out.println(list);

        Assert.check(list.size() == 10);
        Assert.check(list.get(5) == 6);
        Assert.check(list.get(list.size() - 1) == 10);

        Assert.check(list.contains(6));
        Assert.check(!list.contains(5));

        list.set(3, 33);
        System.out.println(list);
        Assert.check(list.get(3) == 33);

        Assert.check(!list.isEmpty());
        list.clear();
//        System.gc();
        Assert.check(list.isEmpty());
    }


}
